package de.java.shortys.myhome.fuelmonitor.validation;

import static de.java.shortys.myhome.fuelmonitor.controller.FuelMonitorController.NAME;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_AMOUNT;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_CHARGES;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_DATE_FILLED;
import static de.java.shortys.myhome.fuelmonitor.model.wrapper.FuelFillingWrapper.ATTR_DISTANCE;
import static de.java.shortys.utils.type.TypeUtils.toType;

import java.util.Calendar;

import de.java.shortys.myhome.api.model.DataHolderDescription;
import de.java.shortys.myhome.api.validation.IValidator;
import de.java.shortys.myhome.api.validation.exception.AttributeFieldNotFoundException;
import de.java.shortys.myhome.api.validation.exception.AttributeInformationFieldNotFoundException;
import de.java.shortys.myhome.fuelmonitor.model.FuelFilling;
import de.java.shortys.myhome.fuelmonitor.model.IFuelFilling;
import de.java.shortys.myhome.fuelmonitor.view.ISubModuleViewFilling;
import de.java.shortys.myhome.keymanager.controller.KeyManager;

public class FillingValidator implements IValidator<IFuelFilling> {

	private ISubModuleViewFilling view;

	private IFuelFilling fuelFilling;

	private boolean edit = true;

	public FillingValidator(ISubModuleViewFilling view) {
		this(view, new FuelFilling());
		edit = false;
	}

	public FillingValidator(ISubModuleViewFilling view, IFuelFilling fuelFilling) {
		this.view = view;
		this.fuelFilling = fuelFilling;
	}

	@Override
	public IFuelFilling getObject() {
		if (!edit) {
			fuelFilling.setKey(KeyManager.getManager(NAME).generateKey());
			fuelFilling.setDateAdded(Calendar.getInstance());
		} else {
			fuelFilling.setDateEdited(Calendar.getInstance());
		}
		return fuelFilling;
	}

	@Override
	public boolean validate() {
		DataHolderDescription dataHolderDescription = fuelFilling.getDescription();
		// FIXME Exception?
		try {
			return validate(ATTR_DATE_FILLED, view.getDateFilled(), dataHolderDescription.getType(ATTR_DATE_FILLED))
					&& validate(ATTR_DISTANCE, view.getDistance(), dataHolderDescription.getType(ATTR_DISTANCE))
					&& validate(ATTR_AMOUNT, view.getAmount(), dataHolderDescription.getType(ATTR_AMOUNT))
					&& validate(ATTR_CHARGES, view.getCharges(), dataHolderDescription.getType(ATTR_CHARGES));
		} catch (AttributeFieldNotFoundException e) {
			e.printStackTrace();
		} catch (AttributeInformationFieldNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}

	private boolean validate(String attributeId, String stringValue, Class<? extends Object> type)
			throws AttributeFieldNotFoundException, AttributeInformationFieldNotFoundException {
		Object value = toType(stringValue, type, true);
		boolean valid = value != null || fuelFilling.isNullable(attributeId);
		view.setValid(attributeId, valid);
		if (valid) {
			fuelFilling.setValue(attributeId, value);
		}
		return valid;
	}

}
